package com.mall.manager.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mall.common.dao.MongoDBManager;
import com.mall.untils.Page;
import com.mongodb.DBObject;
/**
 * 整套题测评统计表业务层
 * @ClassName: GroupTopicService
 * @Description: 
 * @author:wangwenyue wangwenyue@ide365.com
 * @date 2016年7月12日 下午2:28:23
 */
@Service
public class GroupTopicService {

	Logger logger=LoggerFactory.getLogger(GardenService.class);	

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 获取文件数据
	 * @return
	 */
	public List<String> addData(){
		List<String> data = new ArrayList<String>();
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("data_type", "SDK_CEPING_SET");
		List<DBObject> sdkceping = MongoDBManager.find("sdk_collection", map);
		if(sdkceping!=null && sdkceping.size()>0){
			data.addAll(getData(sdkceping));
		}
		
		map.put("data_type", "HTML_CEPING_SET");
		List<DBObject> htmlceping = MongoDBManager.find("html_collection", map);
		if(htmlceping!=null && htmlceping.size()>0){
			data.addAll(getData(htmlceping));
		}
		
		map.put("data_type", "PC_CEPING_SET");
		List<DBObject> pcceping = MongoDBManager.find("pc_collection", map);
		if(pcceping!=null && pcceping.size()>0){
			data.addAll(getData(pcceping));
		}
		
		
		return data;
	}
	
	
	public List<String> getData(List<DBObject> r){
		if(r==null || r.size()<1){
			return null;
		}
		List<String> res =new  ArrayList<String>();
		String resString="";
		Map<String, Object> map =null;
		for (DBObject db : r) {
			map=new HashMap<String, Object>();
			String system_type = String.valueOf(db.get("system_type"));
			
			String baby_id = String.valueOf(db.get("baby_id"));
			String t_time = String.valueOf(db.get("t_time"));

			map.put("baby_id", Long.valueOf(baby_id));
			DBObject findOne = MongoDBManager.findOne("php_baby", map);
			//如果为空 则跳过
			if(findOne==null){
				continue;
			}
			
			
			String babyName = String.valueOf(findOne.get("baby_name"));
			
			String schoolName = String.valueOf(findOne.get("school_name"));
			String schoolId = String.valueOf(findOne.get("school_id"));
			
			String classId = String.valueOf(findOne.get("class_id"));
			String className = String.valueOf(findOne.get("class_name"));
			
			
			map.put("babyName", babyName);
			map.put("schoolName", schoolName);
			map.put("schoolId", schoolId);
			map.put("classId", classId);
			map.put("className", className);
			
			resString="sysType:"+system_type+",t_time:"+t_time+",babyId:"+baby_id+",babyName:"+babyName+",schoolId:"+schoolId+",schoolName:"+schoolName+",classId:"+classId+",className:"+className;
			res.add(resString);
		}
		
		return res;
	}
	
	

	
	/**
	 * 分页查询
	 * @param paraMap  孩子id  园所id  信息来源
	 * @return
	 */
	public Page queryPage(Map<String, Object> paraMap) {
		Page page = null;
		List<Map<String, Object>> queryForList = null;
		int pageNo = MapUtils.getIntValue(paraMap, "pageNo");
		String bady_id = MapUtils.getString(paraMap, "bady_id"); //孩子id
		String garden_id = MapUtils.getString(paraMap, "garden_id");  //园所id
		String data_type = MapUtils.getString(paraMap, "data_type");//信息来源
		int pageSize = 10; //每页显示10条
		Integer total=0; //总数
		try {
			if (pageNo == 0) {
				pageNo = 1;
			}
			pageNo = 2;
			StringBuffer sqlCount = new StringBuffer("select count(*) from t_group_topic_comment where 1=1 ");
			
			if (StringUtils.isNotBlank(bady_id)) {
				sqlCount.append(" and bady_id = ? ");
			}
			
			if (StringUtils.isNotBlank(garden_id)) {
				sqlCount.append(" and garden_id = ? ");
			}
			
			if(StringUtils.isNotBlank(data_type)){
				sqlCount.append(" and data_type = ? ");
			}
			
			
			if(StringUtils.isBlank(bady_id) && StringUtils.isBlank(data_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class);
			}
			if(StringUtils.isBlank(bady_id) && StringUtils.isNotBlank(data_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,data_type);
			}
			if(StringUtils.isNotBlank(bady_id) && StringUtils.isBlank(data_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,bady_id);
			}
			if(StringUtils.isNotBlank(bady_id) && StringUtils.isNotBlank(data_type)){
				total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class,bady_id,data_type);
			}
			
			
			int startIndex = Page.getStartOfPage(pageNo, pageSize);
			StringBuffer sql = new StringBuffer("select * from t_group_topic_comment where 1=1 ");
			if (StringUtils.isNotEmpty(bady_id)) {
				sql.append("and bady_id = ? ");
			}
			if (StringUtils.isNotEmpty(garden_id)) {
				sqlCount.append(" and garden_id = ? ");
			}
			if(StringUtils.isNotBlank(data_type)){
				sqlCount.append(" and data_type = ? ");
			}
			
			String sqlbf = sql.append(" order by id desc limit " + startIndex + "," + pageSize).toString();
			
			if (StringUtils.isNotEmpty(bady_id)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, bady_id);
			}
			if (StringUtils.isNotEmpty(garden_id)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, garden_id);
			}
			if (StringUtils.isNotEmpty(data_type)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, data_type);
			}
			
			queryForList = jdbcTemplate.queryForList(sqlbf);
			page = new Page(startIndex, total, pageSize, queryForList);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
			return null;
		}
	}

}
